package com.aspectj.aop.aspects;


import com.aspectj.aop.annotation.EventTrack;
import com.aspectj.aop.root.AndroidAOP;
import com.aspectj.aop.utils.ArrayUtils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * desc   : 埋点AOP
 * author : chendaning
 * date   : 6/11/21
 */
@Aspect
public class EventTrackAspect extends FatherAspect{
    private final String POINTCUT = "execution(@com.aspectj.aop.annotation.EventTrack * *(..))";

    @Pointcut(POINTCUT)
    public void onEventTrackPointcut() {}

    /**
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("onEventTrackPointcut() && @annotation(eventTrack)")
    public Object execute(ProceedingJoinPoint joinPoint, EventTrack eventTrack) throws Throwable {
        logger("exe EventTrack what=" + eventTrack.what() + ", value=" + ArrayUtils.toString(eventTrack.value()));
        if(AndroidAOP.getmOnEventTrackCallback() != null) {
            AndroidAOP.getmOnEventTrackCallback().onEventTrackCallback(eventTrack.what(), eventTrack.value());
        }
        return joinPoint.proceed();
    }

    public interface OnEventTrackCallback{
        void onEventTrackCallback(int what, String[] value);
    }
}